package com.example.administrator.hqq.View;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2019/1/14 11:27
 * @remark:
 */
@SuppressLint("AppCompatCustomView")
public class RotateImage extends ImageView {
    private ObjectAnimator objectAnimator;
    public static final int STATE_PLAYING =1;//正在播放
    public static final int STATE_PAUSE =2;//暂停
    public static final int STATE_STOP =3;//停止
    public int state;
    public RotateImage(Context context) {
        super(context);
        init();
    }

    public RotateImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RotateImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RotateImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init(){
        objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);
        //添加旋转动画，旋转中心默认为控件中点
        objectAnimator.setDuration(1000);
        //设置动画时间
        objectAnimator.setInterpolator(new LinearInterpolator());
        //动画时间线性渐变
        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE); objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
    }
    public void playAnimator(){
        if (state==STATE_PLAYING)
        {
          stopAnimator();
        }
        else{
            objectAnimator.start();
            state=STATE_PLAYING;
        }
    }
    public void stopAnimator(){
        if (state==STATE_PLAYING)
        {
            objectAnimator.end();
            invalidate();
            state=STATE_STOP;
        }

    }
}
